using System.Collections.Generic;

namespace Msic
{
    public static class ListExtension
    {
        /// <summary>
        /// 尝试获取列表中指定索引的元素
        /// </summary>
        /// <typeparam name="T">列表元素类型</typeparam>
        /// <param name="list">目标列表</param>
        /// <param name="index">要获取的索引</param>
        /// <param name="value">获取到的值</param>
        /// <returns>是否成功获取到值</returns>
        public static bool TryGet<T>(this IList<T> list, int index, out T value)
        {
            if (list != null && index >= 0 && index < list.Count)
            {
                value = list[index];
                return true;
            }

            value = default;
            return false;
        }

        /// <summary>
        /// 尝试获取可枚举集合中指定索引的元素
        /// </summary>
        /// <typeparam name="T">集合元素类型</typeparam>
        /// <param name="enumerable">目标可枚举集合</param>
        /// <param name="index">要获取的索引</param>
        /// <param name="value">获取到的值</param>
        /// <returns>是否成功获取到值</returns>
        public static bool TryGet<T>(this IEnumerable<T> enumerable, int index, out T value)
        {
            if (enumerable != null && index >= 0)
            {
                int currentIndex = 0;
                foreach (var item in enumerable)
                {
                    if (currentIndex == index)
                    {
                        value = item;
                        return true;
                    }
                    currentIndex++;
                }
            }

            value = default;
            return false;
        }

        /// <summary>
        /// 从列表中随机获取指定数量的元素
        /// </summary>
        /// <typeparam name="T">列表元素类型</typeparam>
        /// <param name="list">目标列表</param>
        /// <param name="count">要获取的元素数量，默认为1</param>
        /// <returns>随机获取的元素列表</returns>
        public static List<T> GetRandomElements<T>(this IList<T> list, int count = 1)
        {
            if (list == null || list.Count == 0 || count <= 0)
            {
                return new List<T>();
            }

            // 如果请求数量大于列表长度，返回整个列表
            if (count >= list.Count)
            {
                return new List<T>(list);
            }

            var result = new List<T>();
            var tempList = new List<T>(list);

            // 随机获取指定数量的元素
            for (int i = 0; i < count; i++)
            {
                int randomIndex = UnityEngine.Random.Range(0, tempList.Count);
                result.Add(tempList[randomIndex]);
                tempList.RemoveAt(randomIndex);
            }

            return result;
        }

        /// <summary>
        /// 从列表中随机获取一个元素
        /// </summary>
        /// <typeparam name="T">列表元素类型</typeparam>
        /// <param name="list">目标列表</param>
        /// <returns>随机获取的元素，如果列表为空则返回默认值</returns>
        public static T GetRandomElement<T>(this IList<T> list)
        {
            if (list == null || list.Count == 0)
            {
                return default;
            }

            int randomIndex = UnityEngine.Random.Range(0, list.Count);
            return list[randomIndex];
        }
    }
}
